package com.lp.module_home_a.activity;

import android.graphics.Color;
import android.os.Bundle;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.lp.module_home_a.R;
import com.lp.module_home_a.R2;
import com.lp.module_home_a.adapter.HomeAAdapter;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.base.BaseActivity;
import com.lp.module_lib.base.BaseContent;
import com.lp.module_lib.view.AppTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.dkzwm.widget.srl.SmoothRefreshLayout;

/**
 * 作者：created by LP_TREAM on 2020/11/25 22:28
 * 邮箱：806875201@qq.com
 */
public class HomeAActivity extends BaseActivity {
    @BindView(R2.id.appTitleBar)
    AppTitleBar mAppTitleBar;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    SmoothRefreshLayout mRefreshLayout;

    private HomeAAdapter mAdapter;
    private List<Integer> mBeans = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public SmoothRefreshLayout setRefreshLayout() {
        return mRefreshLayout;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_a_activity_home;
    }

    @Override
    protected void initData() {
        mAppTitleBar.setAppBackground(Color.parseColor("#9400D3"));
        mAppTitleBar.setTitleName("首页");
        mAppTitleBar.setTitleColor(Color.parseColor("#ffffff"));

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        mRecyclerView.setLayoutManager(manager);

        mBeans = new ArrayList<>();
        mAdapter = new HomeAAdapter(R.layout.home_a_item, mBeans);
        mRecyclerView.setAdapter(mAdapter);


        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                manager.invalidateSpanAssignments();//防止第一行到顶部有空白
            }
        });

        getData();
    }


    @Override
    public void getData() {
        int max = 50;
        int min = 1;

        List<Integer> mResult = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int number = (int) (min + Math.random() * max);
            mResult.add(number);
        }

        if (mIndex == BaseContent.baseIndex) {
            mBeans.clear();
        }
        mBeans.addAll(mResult);
        if (mResult.size() < 1) {
            mRefreshLayout.setEnableNoMoreData(true);
        }
        mAdapter.notifyDataSetChanged();
        mRefreshLayout.refreshComplete();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
